package org.algorithm.Stack;

/**
 * 栈的实现
 * @param <E>
 */
public class LinkedListStack<E> implements Stack<E>{

    static class Node<E>
    {
        E value;
        Node<E> next;
        public Node(E value,Node<E> next)
        {
            this.value=value;
            this.next=next;
        }
    }


    public LinkedListStack(int capacity) {
        this.capacity = capacity;
    }

    private int capacity;
    private int size;

    private Node<E> head=  new Node<>(null,null);
    @Override
    public boolean push(E value)
    {
        if (isFull())
        {
            return false;
        }

        head.next=new Node<>(value,head.next);
        size++;
        return true;
    }

    @Override
    public E pop() {
        if (isEmpty())
        {
            return null;
        }

        Node<E> firstNode = head.next;

        head.next=firstNode.next;

        return firstNode.value;
    }

    @Override
    public E peek() {

        return head.next.value;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public boolean isFull() {
        return size==capacity;
    }
}
